package com.atguigu.crowd.mvc.interceptor;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.exception.LoginFailedException;
import com.atguigu.crowd.constant.CrowdConstan;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author zzqstart
 * @Description
 * @date 2022-08-31 1:24
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        HttpSession session = httpServletRequest.getSession();

        Admin admin = (Admin) session.getAttribute(CrowdConstan.ATTR_NAME_LOGIN_ADMIN);

        if (admin == null) {

            throw new LoginFailedException(CrowdConstan.MESSAGE_ACCESS_FORBIDEN);

        }

        return true;
    }


}
